What is child-process-promise?
The 'child-process-promise' npm package is a utility that provides a promise-based interface for Node.js's child_process module. It allows you to execute shell commands and spawn new processes with the added benefit of using promises for better asynchronous control.
What are child-process-promise's main functionalities?
exec
The 'exec' function allows you to run a shell command and returns a promise that resolves with the command's output. This is useful for running simple shell commands and capturing their output.
const { exec } = require('child-process-promise');
exec('ls -la')
.then(result => {
console.log('stdout:', result.stdout);
console.log('stderr:', result.stderr);
})
.catch(err => {
console.error('ERROR:', err);
});
spawn
The 'spawn' function allows you to start a new process using a given command and arguments. It returns a promise that resolves when the process completes. This is useful for more complex scenarios where you need to interact with the process's input/output streams.
const { spawn } = require('child-process-promise');
const child = spawn('ls', ['-la']);
child.childProcess.stdout.on('data', data => {
console.log('stdout:', data.toString());
});
child.childProcess.stderr.on('data', data => {
console.error('stderr:', data.toString());
});
child.then(() => {
console.log('Process completed');
}).catch(err => {
console.error('ERROR:', err);
});
Other packages similar to child-process-promise
execa
Execa is a modern alternative to child_process that provides a promise-based interface for executing shell commands. It offers more features and better defaults compared to child-process-promise, such as improved error handling and support for streaming output.
promisify-child-process
Promisify-child-process is another package that wraps Node.js's child_process module with promises. It provides a similar interface to child-process-promise but focuses on being a lightweight and minimalistic solution.
node-cmd
Node-cmd is a simple package for executing shell commands in Node.js. It provides a callback-based interface but can be easily used with promises using utilities like util.promisify. It is less feature-rich compared to child-process-promise but can be a good choice for simpler use cases.
child-process-promise
Simple wrapper around the child_process
module that makes use of promises
Installation
npm install child-process-promise --save
Usage
exec
var exec = require('child-process-promise').exec;
exec('echo hello')
.then(function (result) {
var stdout = result.stdout;
var stderr = result.stderr;
console.log('stdout: ', stdout);
console.log('stderr: ', stderr);
})
.catch(function (err) {
console.error('ERROR: ', err);
});
spawn
var spawn = require('child-process-promise').spawn;
var promise = spawn('echo', ['hello']);
var childProcess = promise.childProcess;
console.log('[spawn] childProcess.pid: ', childProcess.pid);
childProcess.stdout.on('data', function (data) {
console.log('[spawn] stdout: ', data.toString());
});
childProcess.stderr.on('data', function (data) {
console.log('[spawn] stderr: ', data.toString());
});
promise.then(function () {
console.log('[spawn] done!');
})
.catch(function (err) {
console.error('[spawn] ERROR: ', err);
});
Options
capture
Type: Array
Default: []
Pass an additional capture
option to buffer the result of stdout
and/or stderr
var spawn = require('child-process-promise').spawn;
spawn('echo', ['hello'], { capture: [ 'stdout', 'stderr' ]})
.then(function (result) {
console.log('[spawn] stdout: ', result.stdout.toString());
})
.catch(function (err) {
console.error('[spawn] stderr: ', err.stderr);
});